<?php

namespace app\model;

use think\Model;

class Agent extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'agent';

    public static function is_agent($uid)
    {
        $data = Agent::where(['uid' => $uid, 'status' => 1])->find();
        if (!empty($data)) {
            return 1;
        } else {
            return 0;
        }
    }
    public static function getTitle($uid)
    {
        $data = Agent::where(['uid' => $uid])->find();
        if (!empty($data)) {
            return $data->title;
        }
    }

    public static function register($params)
    {
        $weid = weid();
        $data = Agent::where(['uid' => $params['uid'], 'weid' => $weid])->find();

        if (!empty($data)) {
            Agent::where(['id' => $data->id])->update(['status' => 1]);
        } else {
            $createdata['weid'] = $weid;
            $createdata['title'] = $params['$params'];
            $createdata['agent_level'] = (int) AgentLevel::getdefault()['id'];
            $createdata['create_time'] = time();
            $createdata['status'] = 1;

            Agent::create($createdata);
        }
    }

    public static function goodsShareCharges($order_info, $level)
    {

        $total = 0.00;
        $income = 0;
        $OrderGoods = OrderGoods::where(['order_id' => $order_info['id']])
            ->select()
            ->toArray();
        foreach ($OrderGoods as $vo) {
            if (!empty($vo['goods_id'])) {
                $gscdata = GoodsShareCharges::where(['goods_id' => $vo['goods_id'], 'level' => $level])->find();
                if (!empty($gscdata)) {
                    $gscdata = $gscdata->toArray();
                    if ($gscdata['price'] > 0) {
                        if ($gscdata['charges_method'] == 0) {
                            $income = $income + ($vo['total'] * percent_to_num($gscdata['price']));
                        } elseif ($gscdata['charges_method'] == 1) {
                            $income = $income + $gscdata['price'];
                        }
                    }
                } else {
                    $total += $vo['total'];
                }
            }
        }


        $rdata['income'] = $income;
        $rdata['total'] = $total;

        return $rdata;
    }

    public static function setIncome($uid, $order_info, $percent, $level)
    {
        $Agent = Agent::where('uid', $uid)->find();

        //代理商是状态是开启的
        if (!empty($Agent)) {
            $Agent = $Agent->toArray();
            $gscdata = self::goodsShareCharges($order_info, $level);

            $income = ($gscdata['total'] * percent_to_num($percent)) + $gscdata['income'];
            $return_percent = $percent;

            $agent_level = AgentLevel::find($Agent['agent_level']);
            if (!empty($agent_level)) {
                $agent_level = $agent_level->toArray();
            }

            if (!empty($agent_level['return_percent'])) {
                $income = $income * percent_to_num($agent_level['return_percent']);
                $return_percent = $return_percent * percent_to_num($agent_level['return_percent']);
            }

            if ($income > 0 && $income < 0.01) {
                $income = 0.01;
            }
            Agent::where('uid', $uid)->inc('income', $income)->update();
            Agent::where('uid', $uid)->inc('total_income', $income)->update();

            $incomedata['uid'] = $uid;
            $incomedata['ptype'] = 1;
            $incomedata['weid'] = weid();
            $incomedata['level'] = $level;
            $incomedata['order_id'] = $order_info['id'];
            $incomedata['order_num_alias'] = $order_info['order_num_alias'];
            $incomedata['buyer_id'] = $order_info['uid'];
            $incomedata['income'] = $income;
            $incomedata['return_percent'] = floatval($return_percent);
            $incomedata['percentremark'] = $percent . '% x' . $agent_level['return_percent'] . '%';
            $incomedata['order_total'] = $order_info['total'];
            $incomedata['pay_time'] = $order_info['pay_time'];
            $incomedata['create_time'] = time();
            $incomedata['month_time'] = date('m', time());
            $incomedata['year_time'] = date('Y', time());
            $incomedata['order_status_id'] = Config::getconfig()['paid_order_status_id'];

            Incomelog::create($incomedata);
        }
    }
}
